/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ilookinto.util;

import java.lang.reflect.*;
import java.sql.Connection;

public class CloseUtil {

    private static final String CLOSE = "close";

    /**
     * Invoke the close method in the object. If no close method in the object
     * it does nothing.
     *
     * @param obj the Object with a 'close' method.
     */
    public static void close(Object obj) {
        try {
            if (obj == null) {
                return;
            }
            if (obj instanceof Connection) {
                ((Connection) obj).close();
                return;
            }

            Method method = obj.getClass().getMethod(CLOSE, new Class[]{});
            method.invoke(obj, new Object[]{});
        } catch (Exception ex) {
            // ignore the exception since this should be in the finally block.
        }
    }

    public static void close(Object... args) {
        for (Object obj : args) {
            close(obj);
        }
    }
    //The following is for the JDK 1.4 or before.
    /**
     * Invoke the close method in each object in the array. If no close method
     * in the object it does nothing.
     *
     * @param objs
     *            the array with each object with a close method.
     */
    /*
     * public static void close(Object[] objs) { for (int i = 0; i <
     * objs.length; i++) { close(objs[i]); } }
     */
}
